#!/bin/sh /etc/rc.common

START=95
STOP=15

SERVICE_USE_PID=1
SERVICE_WRITE_PID=1
SERVICE_DAEMONIZE=1

port=$(uci get cdns.@cdns[0].local_port)
enabled=$(uci get cdns.@cdns[0].enable)
automod=$(uci get cdns.@cdns[0].automod)

CONFIG=/etc/cdns/config.json

get_args() {
	config_get_bool enable $1 enable
	config_get local_ip $1 local_ip
	config_get local_port $1 local_port
	config_get server_ip1 $1 server_ip1
	config_get server_ip2 $1 server_ip2
	config_get timeout $1 timeout
}

start_cdns() {
        [ "$enabled" = 1 ] || return 0
	[ "$automod" = 1 ] && {
		uci del dhcp.@dnsmasq[-1].server
		uci del dhcp.@dnsmasq[-1].resolvfile
		uci set dhcp.@dnsmasq[-1].noresolv=1
		uci add_list dhcp.@dnsmasq[-1].server=127.0.0.1#$port
		uci commit dhcp
	}
	mkdir -p $(dirname $CONFIG)
	echo '{"global": {"daemon": true,"log": "syslog:daemon","log_debug": false},"cdns": {' > $CONFIG
	echo '"listen_ip": "'$local_ip'",' >> $CONFIG
        echo '"listen_port": '$local_port',' >> $CONFIG
        echo '"timeout": '$timeout',"servers": [' >> $CONFIG
        echo '{"ip_port": "'$server_ip1'"},' >> $CONFIG
        echo '{"ip_port": "'$server_ip2'"}' >> $CONFIG
        echo ']}}' >> $CONFIG
	ping -w 1 $local_ip && service_start /usr/bin/cdns -c $CONFIG || exit 1
}

start() {
	config_load cdns
	config_foreach get_args cdns
	[ "$enable" = 1 ] && start_cdns
        /etc/init.d/dnsmasq restart
}

stop() {
       [ "$enabled" = '0' ] && {
       [ "$automod" = '1' ] && {
		uci del dhcp.@dnsmasq[-1].server
		uci del dhcp.@dnsmasq[-1].noresolv
		uci set dhcp.@dnsmasq[-1].resolvfile=/tmp/resolv.conf.auto
		uci commit dhcp
	}	
	killall -q cdns
	/etc/init.d/dnsmasq restart
        }
}





